 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=150 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Standardized dialogs
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='StandardEditor,dialog,swing,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Standardized dialogs</div>

<div class='main-body'>
 
<br>Inconsistencies usually decrease the perceived quality of an application.
So, it is probably a good idea to standardize the look and feel of all
dialogs in an application.
<p><b>Example</b>
<p>Here is a class which acts as a template for all dialogs having&nbsp;<code>OK</code>
and&nbsp;<code>Cancel</code> buttons. It standardizes these items :
<ul>
<li>
the form of the dialog title</li>

<li>
the dialog icon</li>

<li>
the layout and spacing of principal elements</li>

<li>
the centering behavior</li>

<li>
the resizing behavior</li>
</ul>
<br>
<PRE>

<span class='keyword'>package</span> hirondelle.stocks.util.ui;

<span class='keyword'>import</span> java.util.*;
<span class='keyword'>import</span> javax.swing.*;
<span class='keyword'>import</span> java.awt.event.*;

<span class='keyword'>import</span> hirondelle.stocks.util.Args;

<span class='comment'>/**
* Abstract Base Class for a dialog with standard layout, buttons, and behavior.
* (The name &lt;tt&gt;StandardEditor&lt;/tt&gt; was chosen since almost all non-trivial 
* dialogs allow the user to edit data in some way.)
*
* &lt;P&gt;Use of this class will apply a standard appearance to 
* dialogs in the application.
*
* &lt;P&gt; Subclasses implement the body of the dialog (wherein business objects 
* are manipulated), and the action taken by the &lt;tt&gt;OK&lt;/tt&gt; button. 
*
* &lt;P&gt;Services of a &lt;tt&gt;StandardEditor&lt;/tt&gt; include: 
*&lt;ul&gt;
* &lt;li&gt;centering on the parent frame
* &lt;li&gt;reusing the parent's icon
* &lt;li&gt;standard layout and border spacing, based on Java Look and Feel guidelines.
* &lt;li&gt;uniform naming style for dialog title, with the application name appearing first
* &lt;li&gt;&lt;tt&gt;OK&lt;/tt&gt; and &lt;tt&gt;Cancel&lt;/tt&gt; buttons at the bottom of the dialog - 
* &lt;tt&gt;OK&lt;/tt&gt; is the default, and the &lt;tt&gt;Escape&lt;/tt&gt; key activates 
* &lt;tt&gt;Cancel&lt;/tt&gt; (the latter works only if the dialog receives the escape 
* keystroke, and not one of its components)
* &lt;li&gt;disabling of resizing
*&lt;/ul&gt;
*
* &lt;P&gt;The &lt;tt&gt;Escape&lt;/tt&gt; key does not always work (for example, when a 
* &lt;tt&gt;JTable&lt;/tt&gt; row has the focus)
*/</span>
<span class='keyword'>public</span> <span class='keyword'>abstract</span> <span class='keyword'>class</span> StandardEditor {
  
  <span class='comment'>/**
  * Constructor.
  * @param aTitle text which appears in the title bar after the name of 
  * the application; must have content.
  * @param aParent window to which this dialog is attached.
  * @param aCloseAction sets the behaviour of the dialog upon close.
  */</span>
  <span class='keyword'>protected</span> StandardEditor (String aTitle, JFrame aParent, CloseAction aCloseAction) {
    Args.checkForContent(aTitle);
    Args.checkForNull(aParent);
    
    fTitle = aTitle;
    fParent = aParent;
    fCloseAction = aCloseAction.getValue();
  }
  
  <span class='comment'>/**
  * Forces calls to constructor to have greater clarity, by using an
  * enumeration instead of integers.
  */</span>
  <span class='keyword'>protected</span> <span class='keyword'>enum</span> CloseAction {
    DISPOSE(JDialog.DISPOSE_ON_CLOSE),
    HIDE(JDialog.HIDE_ON_CLOSE);
    <span class='keyword'>int</span> getValue(){
      <span class='keyword'>return</span> fAction;
    }
    <span class='keyword'>private</span> <span class='keyword'>final</span> <span class='keyword'>int</span> fAction;
    <span class='keyword'>private</span> CloseAction(<span class='keyword'>int</span> aAction){
      fAction = aAction;
    }
  }
  
  <span class='comment'>/**
  * Display this &lt;tt&gt;StandardEditor&lt;/tt&gt; to the user.
  *
  * &lt;P&gt;Follows the Java Look and Feel guidelines for spacing elements.
  */</span>
  <span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>void</span> showDialog(){
    <span class='keyword'>boolean</span> isModal = <span class='keyword'>true</span>;
    fDialog = <span class='keyword'>new</span> JDialog(fParent, UiUtil.getDialogTitle(fTitle), isModal);
    fDialog.setDefaultCloseOperation(fCloseAction);
    fDialog.setResizable(<span class='keyword'>false</span>);
    addCancelByEscapeKey();
    
    JPanel standardLayout = <span class='keyword'>new</span> JPanel();
    standardLayout.setLayout( <span class='keyword'>new</span> BoxLayout(standardLayout, BoxLayout.Y_AXIS) );
    standardLayout.setBorder( UiUtil.getStandardBorder() );
    standardLayout.add( getEditorUI() );
    standardLayout.add( getCommandRow() );
    
    fDialog.getContentPane().add( standardLayout );
    
    UiUtil.centerOnParentAndShow( fDialog );
  }

  <span class='comment'>/** Close the editor dialog.  */</span>
  <span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>void</span> dispose(){
    fDialog.dispose();
  }
  
  <span class='comment'>/**
  * Return the GUI which allows the user to manipulate the business 
  * objects related to this dialog; this GUI will be placed above the 
  * &lt;tt&gt;OK&lt;/tt&gt; and &lt;tt&gt;Cancel&lt;/tt&gt; buttons, in a standard manner.
  */</span>
  <span class='keyword'>protected</span> <span class='keyword'>abstract</span> JComponent getEditorUI();
  
  <span class='comment'>/**
  * The action taken when the user hits the &lt;tt&gt;OK&lt;/tt&gt; button.
  */</span>
  <span class='keyword'>protected</span> <span class='keyword'>abstract</span> <span class='keyword'>void</span> okAction();
  
  <span class='comment'>// PRIVATE //
</span>  <span class='keyword'>private</span> <span class='keyword'>final</span> String fTitle;
  <span class='keyword'>private</span> <span class='keyword'>final</span> JFrame fParent;
  <span class='keyword'>private</span> JDialog fDialog;
  <span class='keyword'>private</span> <span class='keyword'>final</span> <span class='keyword'>int</span> fCloseAction;
  
  <span class='comment'>/**
  * Return a standardized row of command buttons, right-justified and 
  * all of the same size, with OK as the default button, and no mnemonics used, 
  * as per the Java Look and Feel guidelines. 
  */</span>
  <span class='keyword'>private</span> JComponent getCommandRow() {
    JButton ok = <span class='keyword'>new</span> JButton(<span class='literal'>"OK"</span>);
    ok.addActionListener( <span class='keyword'>new</span> ActionListener() {
      <span class='keyword'>public</span> <span class='keyword'>void</span> actionPerformed(ActionEvent event) {
        okAction();
      }
    });
    fDialog.getRootPane().setDefaultButton( ok );
    JButton cancel = <span class='keyword'>new</span> JButton(<span class='literal'>"Cancel"</span>);
    cancel.addActionListener( <span class='keyword'>new</span> ActionListener() {
      <span class='keyword'>public</span> <span class='keyword'>void</span> actionPerformed(ActionEvent event) {
        closeDialog();
      }
    });
    List&lt;JComponent&gt; buttons = <span class='keyword'>new</span> ArrayList&lt;JComponent&gt;();
    buttons.add( ok );
    buttons.add( cancel );
    <span class='keyword'>return</span> UiUtil.getCommandRow( buttons );
  }

  <span class='comment'>/**
  * Force the escape key to call the same action as pressing the Cancel button.
  *
  * &lt;P&gt;This does not always work. See class comment.
  */</span>
  <span class='keyword'>private</span> <span class='keyword'>void</span> addCancelByEscapeKey(){
    String CANCEL_ACTION_KEY = <span class='literal'>"CANCEL_ACTION_KEY"</span>;
    <span class='keyword'>int</span> noModifiers = <span class='literal'>0</span>;
    KeyStroke escapeKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, noModifiers, <span class='keyword'>false</span>);
    InputMap inputMap = 
      fDialog.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
    ;
    inputMap.put(escapeKey, CANCEL_ACTION_KEY);
    AbstractAction cancelAction = <span class='keyword'>new</span> AbstractAction(){
      <span class='keyword'>public</span> <span class='keyword'>void</span> actionPerformed(ActionEvent e){
        closeDialog();
      }
    }; 
    fDialog.getRootPane().getActionMap().put(CANCEL_ACTION_KEY, cancelAction);
  }
  
  <span class='keyword'>private</span> <span class='keyword'>void</span> closeDialog(){
    fDialog.dispose();
  }
}
 
</PRE>
<br>
<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicAction992c-2.html?Id=148'>Look and Feel guidelines</a> <br>
 
  
  <a href='TopicAction2994-2.html?Id=152'>Swing utility tasks</a> <br>
 
  
  <a href='TopicAction11fc-2.html?Id=167'>Input dialogs</a> <br>
 
  
  <a href='TopicAction5982-2.html?Id=228'>Don't subclass JDialog or JFrame</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='150'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=150 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
